﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 使用及对比
{
    public class 性能记数器
    {
        /// <summary>
        /// 开始
        /// </summary>
        public void Start() { }

        #region 记数器各指标

        /// <summary>
        /// 获取所有性能记数器的名称和值列表
        /// </summary>
        /// <param name="CategoryName"></param>
        /// <param name="instanceName"></param>
        public   void 获取所有性能记数器的名称和值列表(string CategoryName, string instanceName)
        {

            string[] instanceNames;
            List<System.Diagnostics.PerformanceCounter> counters = new List<System.Diagnostics.PerformanceCounter>();
            System.Diagnostics.PerformanceCounterCategory mycat = new System.Diagnostics.PerformanceCounterCategory(CategoryName);
            try
            {
                counters.AddRange(mycat.GetCounters(instanceName));
                foreach (System.Diagnostics.PerformanceCounter counter in counters)
                {
                    Console.WriteLine(string.Format("{0,-30}:{1,20}", counter.CounterName, counter.RawValue));
                }
            }
            catch (Exception)
            {
                Console.WriteLine("Unable to list the counters for this category");
            }
        }



        public string GetProcessInstanceName(int pid)
        {
            var category = new PerformanceCounterCategory("Process");

            var instances = category.GetInstanceNames();
            foreach (var instance in instances)
            {

                using (var counter = new PerformanceCounter(category.CategoryName,
                     "ID Process", instance, true))
                {
                    int val = (int)counter.RawValue;
                    if (val == pid)
                    {
                        return instance;
                    }
                }
            }
            throw new ArgumentException("Invalid pid!");
        }


        public   void PrintProcessInfo(string name)
        {
            PerformanceCounter PTCounter = new PerformanceCounter("Process", "% Processor Time", name);
            PerformanceCounter DiskReadCounter = new PerformanceCounter("Process", "IO Read Bytes/sec", name);
            PerformanceCounter DiskWriteCounter = new PerformanceCounter("Process", "IO Write Bytes/sec", name);
            CounterSample cpuSample1 = PTCounter.NextSample();
            CounterSample diskReadSample1 = DiskReadCounter.NextSample();
            CounterSample diskWriteSample1 = DiskWriteCounter.NextSample();
            System.Threading.Thread.Sleep(1000);
            CounterSample cpuSample2 = PTCounter.NextSample();
            CounterSample diskReadSample2 = DiskReadCounter.NextSample();
            CounterSample diskWriteSample2 = DiskWriteCounter.NextSample();
            Console.WriteLine(CounterSampleCalculator.ComputeCounterValue(cpuSample1, cpuSample2));
            Console.WriteLine(CounterSampleCalculator.ComputeCounterValue(diskReadSample1, diskReadSample2));
            Console.WriteLine(CounterSampleCalculator.ComputeCounterValue(diskWriteSample1, diskWriteSample2) / 1024 / 1024 + " MByte/s");

            //内存
            //PerformanceCounter WSCounter = new PerformanceCounter("Process", "Working Set", name);
            PerformanceCounter WSCounter = new PerformanceCounter("Process", "Working Set - Private", name);
            Console.WriteLine("Working Set:" + ((double)WSCounter.NextValue() / 1024 / 1024).ToString());
            //内存最高值
            PerformanceCounter MemeryCounter = new PerformanceCounter("Process", "Working Set Peak", name);
            Console.WriteLine("Working Set Peak:" + ((double)MemeryCounter.NextValue() / 1024 / 1024).ToString());
        }
        #endregion
    }
}
